package com.tyl.unsafa;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class MapTest {
    // java.util.ConcurrentModificationException 并发修改异常
    public static void main(String[] args) {
        //  HashMap<String, String> map = new HashMap<>();在并发中用ArrayList不安全
        /**
         *解决方法
         * 1、Map<Object, Object> map = Collections.synchronizedMap(new HashMap<>());
         * 2、Map<Object, Object> map = new ConcurrentHashMap<>();
         * **/
        Map<Object, Object> map = new ConcurrentHashMap<>();
        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0,10));
                System.out.println(map);
            }).start();
        }
    }
}
